//
//  MallHomeTwoGoodsView.m
//  Yanwei
//
//  Created by weijunjie on 2018/11/16.
//  Copyright © 2018年 DCloud. All rights reserved.
//

#import "MallHomeTwoGoodsView.h"
#import "GoodsModel.h"

@interface MallHomeTwoGoodsView()
@property (nonatomic, strong) UIImageView *goodsImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *currentPriceLabel;
@property (nonatomic, strong) UILabel *lineActionPriceLabel;
@property (nonatomic, strong) UILabel *singleBuyLabel;
@property (nonatomic, strong) UILabel *discountLabel;
/** 团购人数 */
@property (nonatomic, strong) UILabel *groupPurshaseCountLabel;
/** 标签 */
@property (nonatomic, strong) UILabel *markLabel;
@end
@implementation MallHomeTwoGoodsView
- (void)setModel:(GoodsModel *)model {
    _model = model;
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.mainImagePath]];
    self.titleLabel.text = model.title;
    if (model.overseas) { // 跨境商品
        NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc]init];
        muParagraph.lineSpacing = 2;
        muParagraph.firstLineHeadIndent = 35;
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:model.title attributes:@{NSParagraphStyleAttributeName : muParagraph}];
        self.titleLabel.attributedText = attr;
        self.markLabel.hidden = NO;
    } else {
        self.markLabel.hidden = YES;
    }
    
    self.groupPurshaseCountLabel.hidden = YES;
    self.groupPurshaseCountLabel = nil;
    NSString *showPrice = model.sellingPrice;
//    if (model.groupType == 5) { // 正在拼团
//        self.groupPurshaseCountLabel.text = [NSString stringWithFormat:@"  %@人团  ",model.showGroupPeopleNumber];
//        showPrice = model.groupPrice;
//        self.groupPurshaseCountLabel.hidden = NO;
//        self.groupPurshaseCountLabel.textColor = Color175030;
//        self.groupPurshaseCountLabel.layer.borderColor = Color175030.CGColor;
//    }
//    else if (model.showLimitTag){
//        self.groupPurshaseCountLabel.text = @"  限时购  ";
//        showPrice = model.limitSellingPrice;
//        self.groupPurshaseCountLabel.hidden = NO;
//        self.groupPurshaseCountLabel.textColor = Color20812847;
//        self.groupPurshaseCountLabel.layer.borderColor = Color20812847.CGColor;
//    }
//    if ([model.title containsString:@"30起订"]) {
//        NSLog(@"%@",model);
//    }
    
    NSString *currentPrice = [NSString stringWithFormat:@"¥ %@",showPrice];

    NSMutableAttributedString *currentPriceAttr = [[NSMutableAttributedString alloc] initWithString:currentPrice];
    [currentPriceAttr addAttributes:@{NSFontAttributeName : APPFONT(12)} range:NSMakeRange(0, 1)];
    [currentPriceAttr addAttributes:@{NSFontAttributeName : APPBOLDPINGFANGFONT(18)} range:NSMakeRange(1, currentPrice.length - 1)];
    self.currentPriceLabel.attributedText = currentPriceAttr;
    
        NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",model.marketSellingPrice]];
        [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
        self.lineActionPriceLabel.attributedText = newPrice;
    self.discountLabel.text = [NSString stringWithFormat:@"%@折",model.sellingDiscount];
    
}

#pragma mark - 懒加载
- (UIImageView *)goodsImageView {
    if (!_goodsImageView) {
        _goodsImageView = [[UIImageView alloc] init];
        [self addSubview:_goodsImageView];
        [_goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self).offset(5);
            make.right.equalTo(self).offset(-5);
            make.height.mas_equalTo(self.mas_width);
        }];
    }
    return _goodsImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = APPFONT(14);
        _titleLabel.textColor = Color34;
        _titleLabel.numberOfLines = 2;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.goodsImageView.mas_bottom).offset(10);
            make.left.right.equalTo(self.goodsImageView);
        }];
    }
    return _titleLabel;
}

- (UILabel *)currentPriceLabel {
    if (!_currentPriceLabel) {
        _currentPriceLabel = [[UILabel alloc] init];
        _currentPriceLabel.textColor = Color175030;
        [self addSubview:_currentPriceLabel];
        [_currentPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel);
            make.bottom.equalTo(self).offset(-22);
        }];
    }
    return _currentPriceLabel;
}

- (UILabel *)lineActionPriceLabel {
    if (!_lineActionPriceLabel) {
        UIFont *font = APPFONT(10);
        UIColor *color = Color140;
        UILabel *singleBuyLabel = [[UILabel alloc] init];
        singleBuyLabel.font = font;
        singleBuyLabel.textColor = color;
        singleBuyLabel.text = @"市场均价 ";
        [self addSubview:singleBuyLabel];
        _singleBuyLabel = singleBuyLabel;
        [singleBuyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.currentPriceLabel);
            make.bottom.equalTo(self).offset(-7) ;
        }];
        
        _lineActionPriceLabel = [[UILabel alloc] init];
        _lineActionPriceLabel.font = font;
        _lineActionPriceLabel.textColor = color;
        [self addSubview:_lineActionPriceLabel];
        [_lineActionPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(singleBuyLabel.mas_right);
            make.centerY.equalTo(singleBuyLabel);
        }];
    }
    return _lineActionPriceLabel;
}

- (UILabel *)discountLabel {
    if (!_discountLabel) {
        _discountLabel = [[UILabel alloc] init];
        _discountLabel.font = APPFONT(10);
        _discountLabel.textColor = Color140;
        [self addSubview:_discountLabel];
        [_discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.lineActionPriceLabel);
            make.left.equalTo(self.lineActionPriceLabel.mas_right).offset(7);
        }];
    }
    return _discountLabel;
}

/** 商品标题推荐文字 */
- (UILabel *)markLabel {
    if (!_markLabel) {
        NSString *markName = @"跨境";
        _markLabel = [[UILabel alloc] init];
        _markLabel.font = APPFONT(12);
        _markLabel.textAlignment = NSTextAlignmentCenter;
        _markLabel.text = markName;
        _markLabel.layer.borderWidth = 0.5;
        _markLabel.layer.cornerRadius = 1;
        _markLabel.layer.masksToBounds = YES;
        _markLabel.textColor = Color175030;
        _markLabel.layer.borderColor = Color175030.CGColor;
        [self addSubview:_markLabel];
        CGFloat titleH = [markName boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(12)} context:nil].size.height;
        [_markLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel).offset(1);
            make.left.equalTo(self.titleLabel);
            make.width.mas_equalTo(33);
            make.height.mas_equalTo(titleH);
        }];
    }
    return _markLabel;
}

@end
